package com.lsmw.commonutils;
/*
 *ClassName:JwtUtils
 *UserName:二号男嘉宾
 *CreateTime:2020/12/16 9:25
 */

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {
    // 常量
    public static final long EXPIRE = 1000 * 60 * 60 * 24;  // Token过期时间
    public static final String APP_SECRET = "xxisshitukc8BDbRigUDaY6pZFfWus2jZWLPHO";   // 密钥

    // 生成Token字符串的方法
    public static String getJwtToken(String id, String nikename,String auth) {
        String JwtToken = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setSubject("com.oflower-user")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .claim("id", id)
                .claim("nickname", nikename)
                .claim("auth",auth)
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)
                .compact();
        return JwtToken;
    }

    /**
     * 判断token是否存在有效
     *
     * @param jwtToken
     * @return
     */
    public static boolean checkToken(String jwtToken) {
        if (StringUtils.isBlank(jwtToken)) {
            return false;
        }
        try {
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 判断token是否存在与有效
     * @param request
     * @return
     */
    public static boolean checkToken(HttpServletRequest request) {
        try {
            String jwtToken = request.getHeader("token");
            if(StringUtils.isBlank(jwtToken)) return false;
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String getUserIdByJwtToken(HttpServletRequest request){
        String jwtToken = request.getHeader("admin_token");
        if (StringUtils.isBlank(jwtToken)){
            return "";
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        Claims claims = claimsJws.getBody();
        return (String)claims.get("id");
    }

    public static Map getUserIdByJwtToken2(String token) throws Exception{
        if (StringUtils.isBlank(token)){
            return null;
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        Map map = new HashMap<String,String>();
        map.put("id",claims.get("id"));
        map.put("name",claims.get("nickname"));
        map.put("auth",claims.get("auth"));
        return map;
    }

}
